<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <meta http-equiv="content-language" content="zh-CN" />
    

    
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    

    
    <title>非线性规划2</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

    <link rel="stylesheet" href="/css/stylesheet.css">
    <link rel="stylesheet" href="/css/home.css">

    
    
        <style type="text/css">
        body { background-color: #fbf6ec;}
        </style>
    
    
                
        
        
            <link rel="stylesheet" href="/css/main.css"/>
        




        
        
        
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/styles/github.min.css"  />
         
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/highlight.min.js"></script>
        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/r.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/yaml.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/latex.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/matlab.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/mathematica.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia-repl.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/powershell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/bash.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/shell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/python.min.js"></script>
        
        <script>hljs.initHighlightingOnLoad();</script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
     
     
</head>


<body>
    <script>
        window.addEventListener("resize", resizeThrottler, false);

        var resizeTimeout;
        function resizeThrottler() {
        
        if ( !resizeTimeout ) {
            resizeTimeout = setTimeout(function() {
            resizeTimeout = null;
            actualResizeHandler();
        
            
            }, 66);
        }
        }
        actualResizeHandler()
        function actualResizeHandler() {
                if (/mobile/i.test(navigator.userAgent) || /android/i.test(navigator.userAgent))
                {
                    document.body.classList.add('mobile');
                }else{
                    document.body.classList.remove('mobile');  
                }
    }</script>

    
      
      
            <nav class="navbar navbar-default navbar-static-top" style="opacity: .9" role="navigation">
        <div class="container-fluid">
            
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">

                    <span class="sr-only">Toggle navigation</span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>

                </button>
                <a class="navbar-brand" href="/">zsc</a>
            </div>

            <div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1" style="height: auto;">
                <ul class="nav navbar-nav navbar-right" style="font-size: 100%">
                    
                        
                            
                            <li class=""><a href="/about/">About</a></li>
                            
                            <li class=""><a href="/categories/">Categories</a></li>
                            
                            <li class=""><a href="/">Home</a></li>
                            
                            <li class=""><a href="/tags/">Tags</a></li>
                            
                            <li class=""><a href="/issue/">存在的问题</a></li>
                            
                        
                    
                </ul>
            </div>
        </div>
    </nav>







<div class = "div-content" id='div-content-my' style='display: none;' >
    

    <div class = 'inner-left' id= 'divTableOfContents' style="position:fixed;z-index:999;height: 55%;overflow: scroll;bottom: 5%;width: 22%;top: 25%" >
            <p class="slide slidemy" align = "center">
                <a href="javascript:hidediv();" id="strHref" class="btn-slide">目录收起-</a>
            </p>
            
            <div id="divtocTableOfContents">
            <nav id="TableOfContents">
  <ul>
    <li><a href="#rsolnp">Rsolnp</a></li>
    <li><a href="#rdonlp2">Rdonlp2</a></li>
    <li><a href="#matlab-fmincon">Matlab:: fmincon</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#常用非线性规划模型我">常用非线性规划模型(我)</a>
      <ul>
        <li><a href="#r">R</a></li>
        <li><a href="#matlab">Matlab</a></li>
      </ul>
    </li>
  </ul>
</nav>
            </div>
    </div>
</div>
<script>  
    $(document).ready(function () {
    var demo = $("#divtocTableOfContents").find("a").length;
    if(demo > 2){
        $("div#div-content-my").fadeIn("slow");
    }
        
        
        
        
        
        
        
        
    }); 
</script>  









<div class="inner">
    



    <div class="blog-post">
        
                <div>
            <h2 align="center" id = "singe-h2">
                非线性规划2
                <time>
                    <br>
                    <span> 
                        <i class="fa fa-user-edit" style="color:#888;font-size: 80%;"></i>
                        zsc 
                    </span>
                    &nbsp 
                    <span>                 
                        <i class="fa fa-calendar-alt" style="color:#888;font-size: 80%;"></i>
                        2019-06-25 
                    </span>
                </time>
                
                
                <div>
                    <ul class="tags">
                        
                        <span>标签:</span>
                        <li><a class="link" href="/tags/r"> #r </a></li><li><a class="link" href="/tags/matlab"> #matlab </a></li>
                        
                        <span> </span>
                        
                    </ul>
                    
                </div>
            </h2>
        </div>
    
        
        <section id="content">
            <h2 id="rsolnp">Rsolnp</h2>
<p>非线性规划问题</p>
<p>$$
\begin{aligned}
\min \quad &amp; f(x)\
\text { s.t. } &amp; \begin{cases}
{g(x)=0} \{l_{h} \leq h(x) \leq u_{h}} \ {l_{x} \leq x \leq u_{x}}
\end{cases}
\end{aligned}
$$</p>
<p>其中, $f(x),g(x),h(x)$ 都是光滑函数,</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span> <span style="color:#a6e22e">solnp</span>(pars, fun, eqfun <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, eqB <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, ineqfun <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, ineqLB <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, 
</span></span><span style="display:flex;"><span>     ineqUB <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, LB <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, UB <span style="color:#f92672">=</span> <span style="color:#66d9ef">NULL</span>, control <span style="color:#f92672">=</span> <span style="color:#a6e22e">list</span>(), <span style="color:#66d9ef">...</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#75715e"># 参数解释</span>
</span></span><span style="display:flex;"><span>  pars  <span style="color:#f92672">:</span>  初始值(向量), 
</span></span><span style="display:flex;"><span>  fun   <span style="color:#f92672">:</span>  最小化的目标函数值，输入为pars参数，输出为一个单一值,等价上述问题的<span style="color:#a6e22e">f</span>(x)
</span></span><span style="display:flex;"><span>  eqfun <span style="color:#f92672">:</span>  (可选) 等式约束(左边) ，等价与上述问题的 <span style="color:#a6e22e">g</span>(x)
</span></span><span style="display:flex;"><span>  egB <span style="color:#f92672">:</span> (可选) 等式约束右边值，等价上述问题<span style="color:#a6e22e">g</span>(x) <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span> 的<span style="color:#ae81ff">0</span> 
</span></span><span style="display:flex;"><span>  ineqfun <span style="color:#f92672">:</span> (可选) 不等式约束，等价上述问题的 <span style="color:#a6e22e">h</span>(x)
</span></span><span style="display:flex;"><span>  ineqLB <span style="color:#f92672">:</span>(可选) 不等式约束的下限 ，等价于上述问题的 lh
</span></span><span style="display:flex;"><span>  ineqUB <span style="color:#f92672">:</span>(可选) 不等式约束的上限 ，等价于上述问题的 uh
</span></span><span style="display:flex;"><span>  LB <span style="color:#f92672">:</span>(可选) 参变量的下限 ，等价于上述问题的lx
</span></span><span style="display:flex;"><span>  UB<span style="color:#f92672">:</span> (可选) 参变量的上限 ，等价于上述问题的ux 
</span></span><span style="display:flex;"><span>  control <span style="color:#f92672">:</span>(可选) 优化参数控制表。
</span></span></code></pre></div><p>例子：
$$
\begin{aligned}
\min  &amp; \quad exp({\prod_{i=1}^{5}x_i}) \
\text {s.t.} &amp; \begin{cases}
&amp; x_1^2 +x_2^2 +x_3^2+x_4^2+x_5^2 = 10 \
&amp; x_2x_3-5x_4x_5 = 0 \
&amp; x_1^3 +x_2^3 = -1
\end{cases}
\end{aligned}
$$</p>
<pre tabindex="0"><code class="language-{r}" data-lang="{r}"># POWELL Problem
library(Rsolnp)
fn1=function(x){
    exp(x[1]*x[2]*x[3]*x[4]*x[5])
}
eqn1=function(x){
    z1=x[1]*x[1]+x[2]*x[2]+x[3]*x[3]+x[4]*x[4]+x[5]*x[5]
    z2=x[2]*x[3]-5*x[4]*x[5]
    z3=x[1]*x[1]*x[1]+x[2]*x[2]*x[2]
    return(c(z1,z2,z3))
}

x0 = c(-2, 2, 2, -1, -1)
powell=solnp(x0, fun = fn1, eqfun = eqn1, eqB = c(10, 0, -1))
</code></pre><p>具体参考:https://cran.r-project.org/web/packages/Rsolnp/index.html</p>
<h2 id="rdonlp2">Rdonlp2</h2>
<p>这个包有一定的问题，我在运行的时候第一次几乎都正常，能得到正确结果，第二次直接系统崩溃。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">if</span>(<span style="color:#f92672">!</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;Rdonlp2&#39;</span>)) <span style="color:#a6e22e">install.packages</span>(<span style="color:#e6db74">&#34;Rdonlp2&#34;</span>, repos<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;http://R-Forge.R-project.org&#34;</span>)
</span></span></code></pre></div><p>模型格式如下：
$$
\begin{array}{l}{\min z=f(\mathbf{x})} \ {\qquad \begin{array}{l}{\mathrm{x}<em>{1} \leqslant \mathrm{x} \leqslant \mathrm{x}</em>{\mathrm{u}}} \ {\mathrm{b}<em>{1} \leqslant \mathrm{Ax} \leqslant \mathrm{b}</em>{\mathrm{u}}} \ {\mathrm{c}<em>{1} \leqslant \mathrm{c}(\mathrm{x}) \leqslant \mathrm{c}</em>{\mathrm{u}}}\end{array}}\end{array}
$$
具体可参考: (R软件在最优化中的应用)[]</p>
<h2 id="matlab-fmincon">Matlab:: fmincon</h2>
<p>可参考 官网函数解释https://www.mathworks.com/help/optim/ug/fmincon.html</p>
<p><a href="https://wenku.baidu.com/view/6bcb651d0b4e767f5acfce97.html?sxts=1561344526517">https://wenku.baidu.com/view/6bcb651d0b4e767f5acfce97.html?sxts=1561344526517</a></p>
<p><a href="https://blog.csdn.net/qq_38784454/article/details/80329021">https://blog.csdn.net/qq_38784454/article/details/80329021</a></p>
<p>《MATLAB数学建模》李昕——清华大学出版</p>
<h4 id="example">example</h4>
<p>$$
\begin{array}{l}
\min f(x) \
\text {s.t.}\left{\begin{array}{l}
A \cdot x \leq b \
\text {Aeq} \cdot x=beq \
c(x) \leq 0 \
\operatorname{ceq}(x)=0 \
l b \leq x \leq u b
\end{array}\right.
\end{array}
$$
其中 $f(x)$是目标函数, $x, b, beq$是向量,  $A, Aeq$是矩阵, $c(x)$ 和 $ceq(x)$ 是向量函數</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-MATLAB" data-lang="MATLAB"><span style="display:flex;"><span><span style="color:#75715e">% 2.基本语法</span>
</span></span><span style="display:flex;"><span>[x,fval]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 参数解释</span>
</span></span><span style="display:flex;"><span>x的返回值是决策向量x的取值，fval的返回值是目标函数f(x)的取值
</span></span><span style="display:flex;"><span>fun是用M文件定义的函数f(x),代表了(非)线性目标函数
</span></span><span style="display:flex;"><span>x0是x的初始值
</span></span><span style="display:flex;"><span>A,b,Aeq,beq定义了线性约束 ,如果没有线性约束，则A=[],b=[],Aeq=[],beq=[]
</span></span><span style="display:flex;"><span>lb和ub是变量x的下界和上界，如果下界和上界没有约束，则lb=[],ub=[],也可以写成lb的各分量都为 <span style="color:#f92672">-</span>inf,ub的各分量都为inf
</span></span><span style="display:flex;"><span>nonlcon是用M文件定义的非线性向量函数约束
</span></span><span style="display:flex;"><span>options定义了优化参数，不填写表示使用Matlab默认的参数设置
</span></span></code></pre></div><h4 id="例">例</h4>
<p>$$
\begin{aligned}
\min f(x) = x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+8 \
\text { s. t. }\left{\begin{array}{l}
x_{1}^{2}-x_{2}+x_{3}^{2} \geq 0 \
x_{1}+x_{2}^{2}+x_{3}^{2} \leq 20 \
-x_{1}-x_{2}^{2}+2=0 \
x_{2}+2 x_{3}^{2}=3 \
x_{1}, x_{2}, x_{3} \geq 0
\end{array}\right.
\end{aligned}
$$</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>clc,clear all
</span></span><span style="display:flex;"><span>x0 = rand(<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>);  <span style="color:#75715e">% 初始值</span>
</span></span><span style="display:flex;"><span>Aeq = [];               <span style="color:#75715e">% 线性等式约束的系数（左边的系数）</span>
</span></span><span style="display:flex;"><span>beq = [];                <span style="color:#75715e">% 线性等式约束的值 （右边的值）</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%A = [];                % 线性不等式约束的系数</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%b = [];                % 线性等式约束的值</span>
</span></span><span style="display:flex;"><span>ub = [];      <span style="color:#75715e">% 变量的上限（取等号）</span>
</span></span><span style="display:flex;"><span>lb = repelem(<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">3</span>);      <span style="color:#75715e">% 变量的下限（取等号）</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[x,y]=fmincon(@myobjfun,x0,[],[],Aeq,beq,lb,ub,@constrain)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> f = <span style="color:#a6e22e">myobjfun</span>(x)
</span></span><span style="display:flex;"><span>f=x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">8</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [g,h]=<span style="color:#a6e22e">constrain</span>(x)
</span></span><span style="display:flex;"><span>g=[<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>    x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">3</span><span style="color:#f92672">-</span><span style="color:#ae81ff">20</span>]; <span style="color:#75715e">% 不等式</span>
</span></span><span style="display:flex;"><span>h=[<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>    x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">+</span><span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">-</span><span style="color:#ae81ff">3</span>]; <span style="color:#75715e">% 等式约束</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>也可以这样写</p>
<p>(1)编写M函数myobjfun.m 定义目标函数：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> f=<span style="color:#a6e22e">myobjfun</span>(x)
</span></span><span style="display:flex;"><span>	f=x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">8</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>(2)编写M函数constrain.m定义非线性约束条件:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [g,h]=<span style="color:#a6e22e">constrain</span>(x)
</span></span><span style="display:flex;"><span>  g=[<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>      x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">3</span><span style="color:#f92672">-</span><span style="color:#ae81ff">20</span>];
</span></span><span style="display:flex;"><span>  h=[<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>      x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">+</span><span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">-</span><span style="color:#ae81ff">3</span>];
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>(3)编写主程序函数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>[x,y]=fmincon(<span style="color:#e6db74">&#39;fun1&#39;</span>,rand(<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>),[],[],[],[],zeros(<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>),[],<span style="color:#e6db74">&#39;fun2&#39;</span>)
</span></span></code></pre></div><p>运行即可得到结果</p>
<p>参考 : <a href="https://www.cnblogs.com/goodtwo/p/11146540.html">https://www.cnblogs.com/goodtwo/p/11146540.html</a></p>
<h2 id="常用非线性规划模型我">常用非线性规划模型(我)</h2>
<p>$$
\begin{aligned}
Min \quad &amp; \sum_{i=1}^{n} \sum_{j=1}^{n}\left(\omega_{i}-a_{i j} \omega_{j}\right)^{2}\<br>
s.t \quad &amp; \sum_{i=1}^{n} \omega_{i}=1,\
&amp; 1\geq \omega_{i} \geq 0, \quad i=1,2, \ldots, n
\end{aligned}
$$</p>
<p>于是自己写了几个函数模板:</p>
<h3 id="r">R</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(Rsolnp)
</span></span><span style="display:flex;"><span><span style="color:#75715e">## 最小化的目标函数</span>
</span></span><span style="display:flex;"><span>obj<span style="color:#f92672">=</span><span style="color:#a6e22e">function</span>(x){
</span></span><span style="display:flex;"><span>  A1 <span style="color:#f92672">=</span> <span style="color:#a6e22e">matrix</span>(<span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">6</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">3</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">6</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>),ncol <span style="color:#f92672">=</span> <span style="color:#ae81ff">6</span>,nrow <span style="color:#f92672">=</span> <span style="color:#ae81ff">6</span>,byrow <span style="color:#f92672">=</span> T)
</span></span><span style="display:flex;"><span>  s <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>  n <span style="color:#f92672">=</span> <span style="color:#a6e22e">nrow</span>(A1)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">for</span>(i in <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span>n){
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">for</span>(j in <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span>n){
</span></span><span style="display:flex;"><span>      s<span style="color:#f92672">=</span> s<span style="color:#f92672">+</span> (x[i]<span style="color:#f92672">-</span> A1[i,j]<span style="color:#f92672">*</span>x[j])^2;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">return</span>(s)
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#75715e">## 编写解上述非线性规划的函数</span>
</span></span><span style="display:flex;"><span>fmin <span style="color:#f92672">=</span> <span style="color:#a6e22e">function</span>(f,n){<span style="color:#75715e"># n代表参数的个数</span>
</span></span><span style="display:flex;"><span>  eqn1<span style="color:#f92672">=</span><span style="color:#a6e22e">function</span>(x){
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">sum</span>(x)
</span></span><span style="display:flex;"><span>  } <span style="color:#75715e"># 等式约束的左边  ，eqB 为等式约束的右边，如果有多个则用向量</span>
</span></span><span style="display:flex;"><span>  powell<span style="color:#f92672">=</span><span style="color:#a6e22e">solnp</span>(pars<span style="color:#f92672">=</span> <span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span>n,n), fun <span style="color:#f92672">=</span> f, eqfun <span style="color:#f92672">=</span> eqn1, eqB <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>,LB<span style="color:#f92672">=</span> <span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">0</span>,n) ,UB <span style="color:#f92672">=</span> <span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">1</span>,n))
</span></span><span style="display:flex;"><span>  fx <span style="color:#f92672">=</span> powell<span style="color:#f92672">$</span>values
</span></span><span style="display:flex;"><span>  x <span style="color:#f92672">=</span> powell<span style="color:#f92672">$</span>pars
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">return</span>(  <span style="color:#a6e22e">list</span>(<span style="color:#e6db74">&#39;x&#39;</span> <span style="color:#f92672">=</span> x, <span style="color:#e6db74">&#39;fx&#39;</span> <span style="color:#f92672">=</span> fx<span style="color:#a6e22e">[length</span>(fx)]) )
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">fmin</span>(obj,n <span style="color:#f92672">=</span> <span style="color:#ae81ff">6</span>) <span style="color:#75715e">#调用即可，n代表参数的个数</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">########################################</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">library</span>(Rdonlp2)
</span></span><span style="display:flex;"><span>objFun <span style="color:#f92672">=</span> <span style="color:#a6e22e">function</span>(x){
</span></span><span style="display:flex;"><span>  A1 <span style="color:#f92672">=</span> <span style="color:#a6e22e">matrix</span>(<span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">6</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">3</span>,
</span></span><span style="display:flex;"><span>                <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">6</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>),ncol <span style="color:#f92672">=</span> <span style="color:#ae81ff">6</span>,nrow <span style="color:#f92672">=</span> <span style="color:#ae81ff">6</span>,byrow <span style="color:#f92672">=</span> T)
</span></span><span style="display:flex;"><span>  s <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>  n <span style="color:#f92672">=</span> <span style="color:#a6e22e">nrow</span>(A1)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">for</span>(i in <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span>n){
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">for</span>(j in <span style="color:#ae81ff">1</span><span style="color:#f92672">:</span>n){
</span></span><span style="display:flex;"><span>      s<span style="color:#f92672">=</span> s<span style="color:#f92672">+</span> (x[i]<span style="color:#f92672">-</span> A1[i,j]<span style="color:#f92672">*</span>x[j])^2;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">return</span>(s)
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>fmin <span style="color:#f92672">=</span> <span style="color:#a6e22e">function</span>(objFun,n){ <span style="color:#75715e"># 这里n为未知参数的个数.</span>
</span></span><span style="display:flex;"><span>  x0 <span style="color:#f92672">=</span> <span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span>n,n)
</span></span><span style="display:flex;"><span>  lb <span style="color:#f92672">=</span> <span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">0</span>,n) <span style="color:#75715e"># 自变量的下限</span>
</span></span><span style="display:flex;"><span>  ub <span style="color:#f92672">=</span> <span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">1</span>,n) <span style="color:#75715e"># 自变量的上限</span>
</span></span><span style="display:flex;"><span>  Aeq <span style="color:#f92672">=</span> <span style="color:#a6e22e">matrix</span>(<span style="color:#a6e22e">rep</span>(<span style="color:#ae81ff">1</span>,n),<span style="color:#ae81ff">1</span>,n) 
</span></span><span style="display:flex;"><span>  lbeq <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>  ubeq <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>  ret <span style="color:#f92672">=</span> <span style="color:#a6e22e">donlp2</span>(x0,objFun,par.lower <span style="color:#f92672">=</span> lb , par.upper <span style="color:#f92672">=</span> ub,A <span style="color:#f92672">=</span> Aeq,lin.lower <span style="color:#f92672">=</span> lbeq,lin.upper <span style="color:#f92672">=</span> ubeq)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">list</span>(<span style="color:#e6db74">&#39;x&#39;</span> <span style="color:#f92672">=</span> ret<span style="color:#f92672">$</span>par,<span style="color:#e6db74">&#39;objvalue&#39;</span> <span style="color:#f92672">=</span> ret<span style="color:#f92672">$</span>fx)
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">fmin</span>(objFun,n <span style="color:#f92672">=</span> <span style="color:#ae81ff">6</span>)<span style="color:#75715e"># 只需要改写objFun 就可以了</span>
</span></span></code></pre></div><h3 id="matlab">Matlab</h3>
<p>把上述文件存储为<code>fmin.m</code>，以后只需要更改目标函数<code>myobjfun(x,AA)</code>即可,其中x代表未知数, AA 代表目标函数的系数，</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> x = <span style="color:#a6e22e">fmin</span>(AA) <span style="color:#75715e">% 调用函数接口</span>
</span></span><span style="display:flex;"><span>n = size(AA,<span style="color:#ae81ff">1</span>) ; <span style="color:#75715e">% 未知数x的长度 </span>
</span></span><span style="display:flex;"><span>x0 = repelem(<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span>n,n);    <span style="color:#75715e">% 初始迭代位置</span>
</span></span><span style="display:flex;"><span>Aeq = repelem(<span style="color:#ae81ff">1</span>,n);     <span style="color:#75715e">% 线性等式约束的系数（左边的系数）</span>
</span></span><span style="display:flex;"><span>beq = <span style="color:#ae81ff">1</span>;                <span style="color:#75715e">% 线性等式约束的值 （右边的值）</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%A = [];                % 线性不等式约束的系数</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%b = [];                % 线性等式约束的值</span>
</span></span><span style="display:flex;"><span>ub = repelem(<span style="color:#ae81ff">1</span>,n);      <span style="color:#75715e">% 变量的上限（取等号）</span>
</span></span><span style="display:flex;"><span>lb = repelem(<span style="color:#ae81ff">0</span>,n);      <span style="color:#75715e">% 变量的下限（取等号）</span>
</span></span><span style="display:flex;"><span>[x,fval] = fmincon(@(x)myobjfun(x,AA),x0,[],[],Aeq,beq,lb,ub);
</span></span><span style="display:flex;"><span><span style="color:#75715e">%[x,fval] = fmincon(@myobjfun,x0,[],[],Aeq,beq,lb,ub,[],[],options,A);</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 目标函数</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> s = <span style="color:#a6e22e">myobjfun</span>(x,AA)
</span></span><span style="display:flex;"><span>n = size(AA,<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>s =<span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> i = <span style="color:#ae81ff">1</span>:n
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> j = <span style="color:#ae81ff">1</span>:n
</span></span><span style="display:flex;"><span>        s= s<span style="color:#f92672">+</span> (x(i)<span style="color:#f92672">-</span> AA(i,j)<span style="color:#f92672">*</span>x(j))^<span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div>
        </section>
    </div>
    <br>
    
    




<span id="/md/2019-06-24-%E9%9D%9E%E7%BA%BF%E6%80%A7%E8%A7%84%E5%88%92/" class="leancloud_visitors" data-flag-title="非线性规划2">
  <span class="post-meta-item-text">文章总阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次;
  <p></p>
</span>



    

    
    
    <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit" aria-haspopup="true" aria-expanded="false" aria-controls="edit">
        <i class="fa fa-edit">编辑本文</i>
    </button>
    
    
    

    <br>
    <hr>
    <li style="float:left;list-style:none">
        
        <a class="previous" href="/md/2019-05-26-dongtaikeshihua/"> 上一篇: 动态可视化如此简单</a>
        
    </li>
    <li style="float:right;list-style:none">
        
        <a class="next" href="/md/2019-11-04-latex-bib-csl/"> 下一篇: latex &#43; bib &#43; csl</a>
        
    </li>
     
    
    <script src="/js/copyCode.js"></script>
    <script src="/js/tooltips.js"></script>
    
   
    <script>
    [].slice.call(document.querySelectorAll('table')).forEach(function(el) {
        var wrapper = document.createElement('div');
        wrapper.className = 'table-area';
        el.parentNode.insertBefore(wrapper, el);
        el.parentNode.removeChild(el);
        wrapper.appendChild(el);
        $("table").wrap("<div class='table-area'></div>");
    })
    </script>

    
<br>
<hr>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-111691389-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-111691389-1');
</script>




      
      
      

       
      
      
      <script>
              document.getElementById("edit-button").addEventListener("click", function () {
                  var editWindow = window.open("https:\/\/github.com\/zoushucai\/blogmmm/edit/master/content/md\/2019-06-24-非线性规划.md");
              });</script>
      
          




<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  } 
</script>



    </style>
    <script type="text/javascript">
    function showdiv(){
        document.getElementById("divtocTableOfContents").style.display="block";
        document.getElementById("strHref").innerHTML="目录收起-";
        document.getElementById('divTableOfContents').style.width="22%";
        document.getElementById('divTableOfContents').style.height="55%";
        document.getElementById('divTableOfContents').style.top="25%";
        document.getElementById('divTableOfContents').style.bottom="5%";
        document.getElementById("strHref").href="javascript:hidediv()";
    }
    function hidediv(){
        document.getElementById("divtocTableOfContents").style.display="none";
        document.getElementById("strHref").innerHTML="目录展开+";
        document.getElementById("strHref").href="javascript:showdiv()";
        document.getElementById('divTableOfContents').style.width="10%";
        document.getElementById('divTableOfContents').style.height="5%";
    }
    </script>
</body>

</html>
</div> 







    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-LJ2FmexL77rmGm6SIpxq7y+XA6bkLzGZEgCywzKOZG/ws4va9fUVu2neMjvc3zdv" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            renderMathInElement(document.body, {
            delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "$", right: "$", display: false},
                            {left: "\\(", right: "\\)", display: false},
                            {left: "\\[", right: "\\]", display: true}
                        ]
            });
        });
    </script>













<br>
<div class="inner">
              
            
          
          
  
          
  
  <div id="vcomments"></div>
  
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'HfHPKPkLa0cBEDPcdBAHuqMv-gzGzoHsz',
        appKey: 'r5RJAasN8e0mB9sq6y9pEcX0',
        lang:'zh-CN',
        notify:  false , 
        verify:  false  ,
        avatar:'identicon', 
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

</div>

<br>
<br>
<footer>
    <p style="float:right;margin-right: 5%;margin-top: 0%;">
        &copy; 2022 <a href="https://github.com/zoushucai">zsc</a>
      </p>
</footer>
<br>
<br>


